import { GameConfig } from "@/config/GameConfig";

export class BetModel {
    public groupid: number
    public tableid: number;
    public uid: number;
    public bettype: number
    public b1: number;
    public b2: number;
    public pos1: number;
    public pos2: number;
    public num: number;
    public btime: number;

    public getPos1() {
        if (this.bettype == GameConfig.Specail_Action_Type.Nuo) {
            return this.pos1
        }
        return this.b1
    }

    public getPos2() {
        if (this.bettype == GameConfig.Specail_Action_Type.Nuo) {
            return this.pos2
        }
        return this.b2
    }



    private static proto2Model(protoPack) {
        let model: BetModel = new BetModel()
        model.groupid = protoPack["groupid"]
        model.tableid = protoPack["tableid"]
        model.uid = protoPack["uid"]
        model.bettype =  protoPack["bettype"]
        model.b1 = protoPack["b1"]
        model.b2 = protoPack["b2"]
        model.pos1 =  protoPack["pos1"]
        model.pos2 = protoPack["pos2"]
        model.num = protoPack["num"]
        model.btime = protoPack["btime"]
        return model
    }

    private static getBetModelSame(model: BetModel, list: Array<BetModel>) {
        let result: BetModel;
        for (let i = 0; i < list.length; i++) {
            let temp: BetModel = list[i]
            if (model.bettype == temp.bettype) {
                if (model.bettype == GameConfig.Specail_Action_Type.Danya) {
                    if (model.b1 == temp.b1) {
                        result = temp;
                        break
                    }
                }
                else if (model.bettype == GameConfig.Specail_Action_Type.Lianchuan) {
                    if (model.b1 == temp.b1 && model.b2 == temp.b2) {
                        result = temp;
                        break
                    }
                    else if (model.b1 == temp.b2 && model.b2 == temp.b1) {
                        result = temp;
                        break
                    }
                }
                else if (model.bettype == GameConfig.Specail_Action_Type.Baozi) {
                    if (model.b1 == temp.b1) {
                        result = temp;
                        break
                    }
                }
                else if (model.bettype == GameConfig.Specail_Action_Type.Nuo) {
                    if (model.pos1 == temp.pos1 && model.pos2 == temp.pos2) {
                        result = temp;
                        break
                    }
                }
            }
        }
        return result
    }

    public static json2ModelList(protoPackList) {
        let list: Array<BetModel> = []
        for (let i = 0; i < protoPackList.length; i++) {
            let model: BetModel = BetModel.proto2Model(protoPackList[i])
            let samModel: BetModel = this.getBetModelSame(model, list)
            if (samModel) {
                samModel.num += model.num;
            } else {
                list.push(model)
            }
        }
        return list
    }
}